//
//  OSCBottomBarViewController.m
//  OSChina
//
//  Created by 8b410-2 on 17/1/5.
//  Copyright © 2017年 abb. All rights reserved.
//

#import "OSCBottomBarViewController.h"
#import "OSCBottomBarViewModel.h"

#import "OSCRouter.h"
#import "OSCEmoticonViewController.h"
#import "UITextView+OSCEmoticon.h"
@interface OSCBottomBarViewController ()  <OSCPopInputViewDelegate>

@property (nonatomic, strong,readonly)  OSCBottomBarViewModel *viewModel;

@property (nonatomic, strong) UIView *shadowView;

@property (nonatomic,strong) NSAttributedString *beforeATStr;

@property (nonatomic, strong) OSCEmoticonViewController *emoticonVC;

@end

@implementation OSCBottomBarViewController

@dynamic viewModel;

- (instancetype)initWithViewModel:(OSCBottomBarViewModel *)viewModel {
    self = [super initWithViewModel:viewModel];
    if (self) {
        _childVC = [[OSCRouter sharedInstance] viewControllerForViewModel:viewModel.childViewModel];
        [self addChildViewController:_childVC];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = YES;
    [self setupUI];
    [self handleBlock];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillChangeFrame:)
                                                 name:UIKeyboardWillChangeFrameNotification
                                               object:nil];

}
- (void)setupUI {
    _commentView = [[OSCCommentView alloc] initWithPlaceholder:@"123" WithFont:[UIFont systemFontOfSize:13]];
    [self.view addSubview:_childVC.view];
    [self.view addSubview:_commentView];
    [_commentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.mas_equalTo(36);
    }];
    [_childVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.commentView.mas_top);
    }];
}

- (void)handleBlock {
    [_commentView clickTextViewWithAttributeBlock:^(NSAttributedString *text) {
        [self showEditView];
        [self.popInputView restoreDraftNoteWithAttribute:text];
    }];
}

- (void)showEditView {
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    _shadowView = [[UIView alloc] initWithFrame:window.bounds];
    _shadowView.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.5 ];
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchShadowView:)];
    [_shadowView addGestureRecognizer:tapGR];
    [_shadowView addSubview:self.popInputView];
    [self.popInputView activateInputView];
    [window addSubview:_shadowView];
}

- (void)hideEditView{
    self.shadowView.userInteractionEnabled = NO;
    [self.popInputView freezeInputView];
    [UIView animateWithDuration:0.3 animations:^{
        _shadowView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    } completion:^(BOOL finished) {
        [_shadowView removeFromSuperview];
        _shadowView = nil;
    }];
}

- (void)touchShadowView:(UITapGestureRecognizer *)tapGR{
    
    [self hideEditView];
}

- (void)keyboardWillChangeFrame:(NSNotification *)notify {
    
    NSValue *value = [notify.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect rect = [value CGRectValue];
    
    NSNumber *duration = [notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    
    NSNumber *curve = [notify.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    CGFloat y = rect.origin.y == kScreenHeight ? kScreenHeight : rect.origin.y - CGRectGetHeight(self.popInputView.frame);
    
    [UIView animateWithDuration:[duration doubleValue] animations:^{
        [UIView setAnimationCurve:[curve integerValue]];
        self.popInputView.frame = CGRectMake(0, y , kScreenWidth, CGRectGetHeight(self.popInputView.frame));
    }];
    
}

- (OSCEmoticonViewController *)emoticonVC {
    if (!_emoticonVC) {
        _emoticonVC = [[OSCEmoticonViewController alloc] init];
        [_emoticonVC didSelectEmoticonBlock:^(OSCEmoticon *emoticon) {
            [self.popInputView.inputTextView insertEmoticon:emoticon font:[UIFont systemFontOfSize:15]];
        }];
    }
    return _emoticonVC;
}

- (void)popInputViewClickDidEmojiButton:(OSCPopInputView *)popInputView {
    [self.popInputView.inputTextView resignFirstResponder];
    self.popInputView.inputTextView.inputView = (self.popInputView.inputTextView.inputView == nil) ? self.emoticonVC.view : nil;
    [self.popInputView.inputTextView becomeFirstResponder];
}

- (void)popInputViewClickDidSendButton:(OSCPopInputView *)popInputView selectedforwarding:(BOOL)isSelectedForwarding curTextView:(UITextView *)textView {
    self.viewModel.pubCommentParams.content = [textView stringFromEmoticonAttributedWithType:OSCEmoticonStringTypeChs];
    [self.viewModel.requestPubCommentCommand execute:nil];
    [self hideEditView];
}
@end
